#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define str string
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ms(a,t) memset(a,t,sizeof(a))
#define all(x) (x).begin(),(x).end()
#define lowbit(x) ((x)&(-(x)))
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e5+10;
const int maxm = 1e6+10;
const ll mod = 998244353;
const db eps = 1e-6;
int n,m;
ll a[maxn];
ll f[maxm],mx,tg;
ll del;
void Solve() {
	cin >> n; m = 1000000;
	for(int i = 1; i <= m; i++) f[i] = -INF;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	mx = 0; tg = 0;
	for(int i = 2; i <= n; i++) {
		if(a[i] == a[i-1]) tg += a[i];
		else {
			f[a[i-1]] = max(f[a[i-1]],max(mx,f[a[i]]+a[i]));
			mx = max(mx,f[a[i-1]]);
		}
	}
	ll ret = 0;
	for(int i = 1; i <= m; i++) ret = max(ret,f[i]);
	cout<<ret+tg<<'\n';
}
int main() {
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int tt; cin >> tt; while(tt--)
	Solve();
	return 0;
}
